﻿using System;
using System.Web;
using Ext.Net;
using System.Collections.Generic;
using System.Collections;
using Film.Repository;

namespace PhuongDoAn.Admin.Film.Handler
{
    /// <summary>
    /// Summary description for ListHandler
    /// </summary>
    public class ListHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string query = "";
            int start = 0;
            int limit = 25;
            int count = 0;
            string collapse = "";
            if (context.Request["collapse"] != null)
            {
                collapse = context.Request["collapse"].ToString();
            }

            if (collapse.ToLower() == "false")
            {
                if (!string.IsNullOrEmpty(context.Request["type"]))
                {
                    query = context.Request["type"];

                }
            }
            else
            {
                if (!string.IsNullOrEmpty(context.Request["keyWord"]))
                {
                    query = context.Request["keyWord"];
                }
            }


            if (!string.IsNullOrEmpty(context.Request["start"]))
            {
                int.TryParse(context.Request["start"], out start);
            }

            if (!string.IsNullOrEmpty(context.Request["limit"]))
            {
                int.TryParse(context.Request["limit"], out limit);
            }

            List<tblMovie> result = new List<tblMovie>();
            tblMovieRepository userAccountRepository = new tblMovieRepository();

            tblMovie entity = new tblMovie();

            result = userAccountRepository.Filter(query, start, limit, out count, null);
            List<tblMovieHandler> listHandler = result.ConvertAll<tblMovieHandler>(delegate(tblMovie e)
            {
                return new tblMovieHandler() { MovieID = e.MovieID, MovieName = e.MovieName, Director = e.Director, ImageUrl = e.ImageUrl, Status = e.Status };
            });
          //  context.Response.Write("tuannv");
         //   return;
            context.Response.Write(string.Format("{{'Data':{0},'TotalRecords':{1}}}", JSON.Serialize(listHandler), count));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

    public class tblMovieHandler
    {
        public int MovieID { get; set; }
        public string MovieName { get; set; }
        public string Director { get; set; }
        public bool Status { get; set; }
        public string ImageUrl { get; set; }
    }
}